package com.tianzmp.ttq.admin.util;

import com.tianzmp.ttq.admin.exception.AdminBusinessException;
import com.tianzmp.ttq.admin.exception.ErrorEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable;

import java.util.Collection;
import java.util.Map;

/**
 * 参数校验
 *
 * @author zhump
 * @version 1.0.0.1
 * @className AssertUtils
 * @description TODO
 * @date 2021/10/26 10:10
 */
public class AssertUtils {


    /**
     * 不为空-----> 抛异常
     *
     * @param obj
     * @param error
     */
    public static void isNotNull(Object obj, ErrorEnum error) {
        if (obj != null && obj.toString().length() > 0) {
            throw new AdminBusinessException(error.getCode(),error.getMsg());
        }
    }

    /**
     * 为空----抛异常
     *
     * @param obj
     * @param error
     */
    public static void isNull(Object obj, ErrorEnum error) {
        if (obj == null || obj.toString().length() == 0) {
            throw new AdminBusinessException(error.getCode(),error.getMsg());
        }
    }

    /**
     * 参数异常
     *
     * @param strings
     */
    public static void isNull(ErrorEnum error,String... strings) {
        if (StringUtils.isAnyBlank(strings)) {
            throw new AdminBusinessException(error.getCode(),error.getMsg());
        }
    }

    /**
     * 是否为 TRUE -----抛异常
     *
     * @param expression
     * @param error
     */
    public static void checkTrue(boolean expression, ErrorEnum error) {
        if (expression) {
            throw new AdminBusinessException(error.getCode(),error.getMsg());
        }
    }

    /**
     * 长度
     *
     * @param text
     * @param error
     */
    public static void hasLength(@Nullable String text, ErrorEnum error) {
        if (StringUtils.isBlank(text) || text.length() == 0) {
            throw new AdminBusinessException(error.getCode(),error.getMsg());
        }
    }

    /**
     * 集合判断
     *
     * @param collection
     * @param error
     */
    public static void notEmpty(@Nullable Collection<?> collection, ErrorEnum error) {
        boolean b = collection == null || collection.isEmpty();
        if (b) {
            throw new AdminBusinessException(error.getCode(),error.getMsg());
        }
    }

    /**
     * map 判断
     * 没有采用 CollectionUtils 判断，是因为底层也就一行代码
     * 所以没有采用，就直接判断了。
     *
     * @param map
     * @param error
     */
    public static void notEmpty(@Nullable Map<?, ?> map, ErrorEnum error) {
        boolean b = map == null || map.isEmpty();
        if (b) {
            throw new AdminBusinessException(error.getCode(),error.getMsg());
        }
    }
}
